package org.springblade.modules.admin.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.admin.entity.ContractAgree;
import org.springblade.modules.admin.service.IContractAgreeService;
import org.springblade.modules.admin.vo.contract.ContractAgreeStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zcc
 * @version 1.0
 * @date 2025/3/28 16:20
 */
@Slf4j
@Component
public class ContractAgreeJob {
	/**
	 * "协议状态1:待回签2:待审核3:待生效4:待完结(已生效)5:合同待回签6:合同已回签7:审核不通过8:已终止9:已作废10:已关闭",11取消作废，12：取消关闭
	 */
	private final Integer[] AGREE_STATUS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

	@Autowired
	private IContractAgreeService contractAgreeService;

	/**
	 * 更新合同协议的状态
	 */
	@Scheduled(cron = "0 0/1 * * * ?")
	@XxlJob(value = "updateContractAgree")
	public void updateContractAgree() {
		XxlJobHelper.handleSuccess("协议管理:更新协议状态JOB 开始执行......");
		List<ContractAgree> list = contractAgreeService.list(Wrappers.<ContractAgree>lambdaQuery().eq(ContractAgree::getStatus, 2).eq(ContractAgree::getIsDrafts, 0));
		List<ContractAgree> updateList = new ArrayList<>(16);
		XxlJobHelper.log("协议管理:执行日志获取待处理的数据数" + list.size());
		for (ContractAgree contractAgree : list) {
			if (contractAgree.getStopDate() != null) {
				if (!contractAgree.getAgreeStatus().equals(AGREE_STATUS[8])) {
					if (System.currentTimeMillis() > contractAgree.getStopDate().getTime()) {
						contractAgree.setAgreeStatus(AGREE_STATUS[8]);
						updateList.add(contractAgree);
					}
				}
			} else {
				if (!contractAgree.getAgreeStatus().equals(AGREE_STATUS[6]) && !contractAgree.getAgreeStatus().equals(AGREE_STATUS[5])) {
					if (contractAgree.getEndTime().getTime() < System.currentTimeMillis()) {
						contractAgree.setId(contractAgree.getId());
						contractAgree.setAgreeStatus(AGREE_STATUS[5]);
						List<ContractAgreeStatusVo> agreeStatusList = JSON.parseArray(JSON.toJSONString(contractAgree.getAgreeStatusList()), ContractAgreeStatusVo.class);
						for (ContractAgreeStatusVo contractAgreeStatusVo : agreeStatusList) {
							if (contractAgreeStatusVo.getStatus().equals(AGREE_STATUS[5])) {
								contractAgreeStatusVo.setUpdateDate(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
								contractAgreeStatusVo.setCreateUserName("系统更新");
							}
						}
						contractAgree.setAgreeStatusList(agreeStatusList);
						updateList.add(contractAgree);
						continue;
					}
				}

				if (!contractAgree.getAgreeStatus().equals(AGREE_STATUS[6]) && !contractAgree.getAgreeStatus().equals(AGREE_STATUS[4])) {
					if (contractAgree.getBeginTime().getTime() < System.currentTimeMillis() && contractAgree.getEndTime().getTime() > System.currentTimeMillis()) {
						contractAgree.setId(contractAgree.getId());
						contractAgree.setAgreeStatus(AGREE_STATUS[4]);
						List<ContractAgreeStatusVo> agreeStatusList = JSON.parseArray(JSON.toJSONString(contractAgree.getAgreeStatusList()), ContractAgreeStatusVo.class);
						for (ContractAgreeStatusVo contractAgreeStatusVo : agreeStatusList) {
							if (contractAgreeStatusVo.getStatus().equals(AGREE_STATUS[4])) {
								contractAgreeStatusVo.setUpdateDate(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
								contractAgreeStatusVo.setCreateUserName("系统更新");
							}
						}
						contractAgree.setAgreeStatusList(agreeStatusList);
						updateList.add(contractAgree);
					}
					continue;
				}
			}
		}
		if (updateList.size() > 0) {
			List<List<ContractAgree>> partition = Lists.partition(updateList, 20);
			for (List<ContractAgree> up : partition) {
				contractAgreeService.updateBatchById(up);
			}
		}


		XxlJobHelper.handleSuccess("协议管理:更新协议状态JOB 执行结束......");
	}
}
